package com.todo.strategy.card.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.strategy.card.infrastructure.entity.condition.TravelCardCondition;
import com.todo.strategy.card.infrastructure.entity.model.TravelCardCountModel;
import com.todo.strategy.card.infrastructure.entity.po.TravelCardPO;
import com.todo.strategy.card.infrastructure.mapper.TravelCardMapper;
import com.todo.strategy.card.infrastructure.repository.TravelCardRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TravelCard)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("travelCardRepository")
public class TravelCardRepositoryImpl implements TravelCardRepository {

    @Resource
    private TravelCardMapper travelCardMapper;


    @Override
    public PageResult<TravelCardPO> queryTravelCardByCondition(TravelCardCondition travelCardCondition) {
        PageResult<TravelCardPO> pageResult = new PageResult<>();
        if (travelCardCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(travelCardCondition.getPageIndex());
        pageResult.setPageSize(travelCardCondition.getPageSize());
        long count = queryCountByCondition(travelCardCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, travelCardCondition.getPageSize()));
        pageResult.setTotal(count);
        List<TravelCardPO> travelCardPOList = travelCardMapper.queryTravelCardByCondition(travelCardCondition);
        if (CollectionUtils.isEmpty(travelCardPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(travelCardPOList);
        pageResult.setCurrentIndex(travelCardPOList.get(0).getId());
        pageResult.setHasNext(travelCardPOList.size() >= travelCardCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(TravelCardCondition travelCardCondition) {
        if (travelCardCondition == null || travelCardCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return travelCardMapper.queryCountByCondition(travelCardCondition);
    }

    @Override
    public TravelCardPO queryTravelCardPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return travelCardMapper.queryTravelCardPOById(id);
    }

    @Override
    public Boolean insertTravelCardPO(TravelCardPO travelCardPO) {
        if (travelCardPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(travelCardPO)) {
            return false;
        }
        travelCardPO.setBoughtCount(0);
        travelCardPO.setSiteCount(0);
        return travelCardMapper.insertTravelCardPO(travelCardPO) > 0;
    }

    private boolean baseFieldCheck(TravelCardPO travelCardPO) {
        if (travelCardPO == null) {
            return false;
        }
        if (travelCardPO.getTitle() == null || travelCardPO.getDesc() == null || travelCardPO.getUseRule() == null) {
            log.error("TravelCardRepository#baseFieldCheck 核心字段校验失败，数据异常，不做处理 travelCardPO：【{}】", JSONObject.toJSONString(travelCardPO));
            return false;
        }
        if (travelCardPO.getFixPrice() == null || travelCardPO.getSellPrice() == null || travelCardPO.getSellPrice() > travelCardPO.getFixPrice()) {
            log.error("TravelCardRepository#baseFieldCheck 金额字段校验失败，数据异常，不做处理 travelCardPO：【{}】", JSONObject.toJSONString(travelCardPO));
            return false;
        }
        return true;
    }

    @Override
    public Boolean updateTravelCardPOById(TravelCardPO travelCardPO) {
        if (travelCardPO == null || travelCardPO.getId() == null || travelCardPO.getId() < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(travelCardPO)) {
            return false;
        }
        return travelCardMapper.updateTravelCardPOById(travelCardPO) > 0;
    }

    @Override
    public Boolean removeTravelCardPOById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        return travelCardMapper.removeTravelCardPOById(id) > 0;
    }

    @Override
    public Boolean updateTravelCardCount(TravelCardCountModel model) {
        if (model == null || model.getId() == null || model.getId() < 0){
            return false;
        }
        return travelCardMapper.updateTravelCardCount(model);
    }
}
